local Lang = require("common.Lang")
local Util = require("common.Util")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local DailyTaskExcel = require("excel.dailyTask")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local Log = require("common.Log")
local PfLogic = require("platform.PfLogic")


DAILY_SPECIAL_TASK_ID_1  = 1  --每日分享任务
  

-- 查询每日任务
function dailyTaskQuery(human)
	local dailyTaskConfig = DailyTaskExcel.shareTask
	if not dailyTaskConfig then return end
	
	-- 每日更新
	ObjHuman.updateDaily(human)
	
	local msgRet = Msg.gc.GC_DAILYSHARE_QUERY 
	local now = os.time()
	local ts1 = Util.getDayStartTime(now)
	msgRet.nextRefreshTime = 24 * 60 * 60 - (now - ts1)
	
	local cnt = 0
	local len = 0
	for key, value in ipairs(DailyTaskExcel.shareTask) do 
		cnt = cnt + 1
		msgRet.list[cnt].taskID = key
		msgRet.list[cnt].desc = value.taskDesc
		msgRet.list[cnt].maxCnt = value.taskCnt
		
		len = 0
		for k, v in pairs(value.reward) do
			len = len + 1
			Grid.makeItem(msgRet.list[cnt].itemData[len], v[1], v[2])
		end
		msgRet.list[cnt].itemData[0] = len
		
		msgRet.list[cnt].finishCnt = 0
		msgRet.list[cnt].isGet = 0
		if human.db.dailyShareTask and human.db.dailyShareTask[key] then
			msgRet.list[cnt].finishCnt = human.db.dailyShareTask[key].finishCnt or 0
			if human.db.dailyShareTask[key].isGet then 
				msgRet.list[cnt].isGet = 1
			end
		end
	end
	
	msgRet.list[0] = cnt
	Msg.send(msgRet, human.fd)
end

-- 记录每个任务的完成次数
function recordDailyTaskFinishCnt(human, msg)
	local taskID = msg.taskID
    local addCnt = 1
	local pf = msg.pf
	local appid = msg.appid
	local shareType = msg.shareType
	
	if taskID ~= 1 then 
		return
	end
	
	local dailyTaskConfig = DailyTaskExcel.shareTask[taskID]
	if not dailyTaskConfig then return end

	-- 每日更新
	ObjHuman.updateDaily(human)
	
	human.db.dailyShareTask = human.db.dailyShareTask or {}
	human.db.dailyShareTask[taskID] = human.db.dailyShareTask[taskID] or {}
	human.db.dailyShareTask[taskID].finishCnt = human.db.dailyShareTask[taskID].finishCnt or 0
	if human.db.dailyShareTask[taskID].finishCnt >= dailyTaskConfig.taskCnt then
		dailyTaskQuery(human)
		return
	end
	human.db.dailyShareTask[taskID].finishCnt = human.db.dailyShareTask[taskID].finishCnt + addCnt
	
	--'1:微信好友分享,2:微信朋友圈,3:微博,4:QQ,5:QQ好友,6:其他'
	Log.write(Log.LOGID_OSS_SHARE, human.db._id, human.db.account, human.db.name, pf, appid, shareType)
	
	-- 领奖品
	BagLogic.addItemList(human, dailyTaskConfig.reward, "daily_task")     	
	-- 通知客户端
	dailyTaskQuery(human)
end

function isOpen(human)
	-- 目前只有H5渠道开，但H5是客户端拉起聚合的接口来判断是否开
	-- if PfLogic.isKunTangTishen() == true or PfLogic.isFkwTishen() == true then
	-- 	return
	-- end

	-- if PfLogic.isKunTang(human) == true or PfLogic.isFKW(human) == true then
	-- 	return true
	-- end
end
